package com.moyuxi.sound_of_the_forest.ui.compose

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.moyuxi.sound_of_the_forest.R
import com.moyuxi.sound_of_the_forest.entity.LocalMusicBean


/**
 * 歌曲列表
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SongListCompose(
    beans: List<LocalMusicBean>, /*mainVM: MainViewModel*/
    playingAllClick:(List<LocalMusicBean>)->Unit,
    playingInBean:(LocalMusicBean)->Unit,
    addToPlayingList:(LocalMusicBean)->Unit,
    removeFromDataList:(LocalMusicBean)->Unit,
) {
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        // 顶部功能栏
        Row(verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .clickable { playingAllClick(beans.toMutableList()) }
                .fillMaxWidth()
                .background(MaterialTheme.colorScheme.primaryContainer)
                .padding(10.dp)) {
            Image(
                painter = painterResource(id = R.drawable.icon_play_all),
                contentDescription = "播放全部",
                modifier = Modifier.size(35.dp)
            )
            Spacer(modifier = Modifier.width(5.dp))
            Text(text = "播放全部(${beans.size})")
            // 多选以及排序的Compose
        }
        // 歌曲列表
        LazyColumn(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth(),
            contentPadding = PaddingValues(10.dp, 10.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            items(beans) {
                var expanded by remember { mutableStateOf(false) }
                val items = listOf("添加到播放列表", "移出列表")
                Card(
                    shape = MaterialTheme.shapes.large,
                    modifier = Modifier
                        .fillMaxWidth()
                        .combinedClickable(onClick = { playingInBean(it) },
                            onLongClick = { expanded = true })
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier.padding(all = 5.dp),
                    ) {
                        // 图片位置，无障碍说明，大小50dp平方像素，裁剪为圆形，边框厚度1.5dp配置颜色形状圆形。
                        val modifier = Modifier
                            .padding(3.dp)
                            .size(50.dp)
                            .clip(MaterialTheme.shapes.large)
                            .border(
                                1.dp,
                                MaterialTheme.colorScheme.secondary,
                                MaterialTheme.shapes.large
                            )
                        MusicImageCompose(it.path, modifier)
                        Spacer(Modifier.padding(horizontal = 5.dp))
                        Column() {
                            Text(text = it.name)
                            Spacer(Modifier.padding(horizontal = 3.dp))
                            Text(text = it.singer)
                        }
                    }
                    DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
                        items.forEachIndexed { index, label ->
                            DropdownMenuItem(text = { Text(text = label) }, onClick = {
                                expanded = false
                                if (index == 0)addToPlayingList(it)
                                else if (index == 1) removeFromDataList(it)
                            })
                        }
                    }
                }
            }
        }
    }
}